<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui_ext/xmSelect/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title>用户列表</title>
</head>
<body>
<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-username"></i>
            </div>
            <input type="text" name="username" value="" placeholder="用户名" class="layui-input" lay-affix="clear">
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <input type="text" name="idcard" placeholder="身份证号" lay-affix="clear" class="layui-input">
        </div>
    </div>

    <div class="layui-btn-container layui-col-xs12">
        <button class="layui-btn" lay-submit lay-filter="demo-table-search">Search</button>
        <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
    </div>
</form>
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="recharge" style="display:{{d.status=='启用'?'':'none'}} ">充值</a>
    </div>
</script>

<button class="layui-btn" lay-on="e1">导出所有到excel</button>
<button class="layui-btn" lay-on="e2">导出选中到excel</button>
<table class="layui-hide" id="ID-table-demo-search"></table>
<script type="text/html" id="ID-table-demo-templet-switch">
    <!-- 这里的 checked 的状态值判断仅作为演示 -->
    <input type="checkbox" name="status" value="{{= d.id }}" title="启用|停用" lay-skin="switch"
           lay-filter="demo-templet-status" {{=d.status == "启用" ? "checked" : "" }}>
</script>

<script>
    layui.use(function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var util = layui.util;

        util.on({
            e1: function () {
                location.href = "/user/exportAllExcel.do"

            }, e2: function () {
                //获取选中的行ID
                var checkStatus = table.checkStatus('ID-table-demo-search')
                var data = checkStatus.data; // 获取选中的数据
               let ids= data.map(function(item){
                    return item.id
                }).join(',');
               if(ids==''){
                   layer.msg("请选择要导出的数据", {time: 2000});

               }else {
                   location.href = "/user/exportExcelByCondition.do?ids=" + ids;
               }
            }
        });
        // 触发单元格工具事件
        table.on('tool(ID-table-demo-search)', function (obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据

            if (obj.event === 'edit') {
                location.href = "/user/toedit.do?id=" + data.id;
            } else if (obj.event === 'recharge') {
                location.href = "/user/toRecharge.do?id=" + data.id + "&username=" + data.username;
            }
        })

        // 状态 - 开关操作
        form.on('switch(demo-templet-status)', function (obj) {
            var id = this.value;
            //      var name = this.name;
            let status = obj.elem.checked == true ? "启用" : "禁用";
            $.post("/user/updateStatus.do", {"id": id, "status": status}, function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {time: 2000}, function () {
                        location.href = "/user/tolist.do"
                    })
                } else {
                    layer.msg(res.msg, {time: 2000})
                }
            })
            //  layer.tips(id + ' ' + name + ': '+ obj.elem.checked, obj.othis);
        });

        //AES 解密身份证
        function parseData(idcard) {
            var key = CryptoJS.enc.Utf8.parse('1234567890123456'); // 16位密钥
            // 使用 CryptoJS 的 AES 解密方法

            var result = CryptoJS.AES.decrypt(idcard, key, {
                mode: CryptoJS.mode.ECB,//使用 ECB 模式（电子密码本模式）
            });
            return result.toString(CryptoJS.enc.Utf8);

        }

        // 创建表格实例
        table.render({
            elem: '#ID-table-demo-search',
            url: '/user/list.do', // 此处为静态模拟数据，实际使用时需换成真实接口
            parseData: function (res) {
                return {
                    code: res.code,
                    data: res.data.list,
                    count: res.data.total,
                }
            }, limit: 3,
            cols: [[
                {type: 'checkbox', title: '😊', fixed: true}, // 单选框
                {field: 'id', title: 'ID', width: 80, sort: true, fixed: true},
                {field: 'username', title: '用户名', width: 80},
                {field: 'password', title: '密码', width: 80, sort: true},
                {field: 'phone', title: '手机号', width: 180},
                {field: 'balance', title: '账户余额', width: 180},
                {field: 'status', title: '状态', width: 180, templet: '#ID-table-demo-templet-switch'},
                {
                    field: 'idcard', title: '身份证号', width: 180, templet: function (d) {
                        return parseData(d.idcard);
                    }
                },
                {
                    field: 'header', title: '头像', width: 180, templet: function (res) {
                        return "<img src='" + res.header + "' alt='' width='50px' height='50px'/>"
                    }
                },
                {fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#toolDemo'}
            ]],
            page: true,
            height: 310
        });

        // 日期
        laydate.render({
            elem: '.demo-table-search-date'
        });
        // 搜索提交
        form.on('submit(demo-table-search)', function (data) {
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('ID-table-demo-search', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
            });
            layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
            return false; // 阻止默认 form 跳转
        });
    });
</script>

</body>
</html>